package test.excelHttpsample;

import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.converters.SampleResultConverter;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
/**
 * 这个类参考的是HTTPResultConverter
 * @author Administrator
 *
 */
public class HTTPBaseTestSampleResultConverter extends SampleResultConverter{

	public HTTPBaseTestSampleResultConverter(Mapper arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}
	
	
	public static String getVersion(){
		return "$Revision: 1641775 $";  //$NON-NLS-1$
	}
	
	
	@Override
	public boolean canConvert(@SuppressWarnings("rawtypes")Class arg0) {
		return HTTPBaseSampleResult.class.equals(arg0);
	}
	
	@Override
	public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
		HTTPBaseSampleResult res = (HTTPBaseSampleResult)obj;
		 SampleSaveConfiguration save = res.getSaveConfig();
	        setAttributes(writer, context, res, save);
	        saveAssertions(writer, context, res, save);
	        saveSubResults(writer, context, res, save);
	        saveResponseHeaders(writer, context, res, save);
	        saveRequestHeaders(writer, context, res, save);
	        saveResponseData(writer, context, res, save);
	        saveSamplerData(writer, context, res, save);
	}
	/**
	 * 这里暂时没有，不需要和httpresult一样加入cookie,method什么的
	 */
	@Override
	protected void saveSamplerData(HierarchicalStreamWriter writer, MarshallingContext context, SampleResult res,
			SampleSaveConfiguration save) {
		// TODO Auto-generated method stub
		super.saveSamplerData(writer, context, res, save);
	}
	
	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		 HTTPBaseSampleResult res = (HTTPBaseSampleResult) createCollection(context.getRequiredType());
	        retrieveAttributes(reader, context, res);
	        while (reader.hasMoreChildren()) {
	            reader.moveDown();
	            Object subItem = readItem(reader, context, res);
	            retrieveItem(reader, context, res, subItem);
	            reader.moveUp();
	        }

	        // If we have a file, but no data, then read the file
	        String resultFileName = res.getResultFileName();
	        if (resultFileName.length()>0
	        &&  res.getResponseData().length == 0) {
	            readFile(resultFileName,res);
	        }
	        return res;
	}
	
}
